package com.lbs.jiepiaisi.config;

import com.lbs.jiepiaisi.utils.ResponseConverterFactory;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

public class Api {

    private Retrofit retrofit;
    public ApiService service;
    private static Api instance;

    public static Api getInstance() {
        return new Api();
    }

    public Api(){
//        OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
        //设置请求超时时长
//        okHttpClientBuilder.connectTimeout(30, TimeUnit.SECONDS);
        //启用Log日志
//        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
//        interceptor.level(HttpLoggingInterceptor.Level.BODY);
//        okHttpClientBuilder.addInterceptor(interceptor);
        //设置缓存方式、时长、地址
//        okHttpClientBuilder.addNetworkInterceptor(getCacheInterceptor());
//        okHttpClientBuilder.addInterceptor(getCacheInterceptor());
//        okHttpClientBuilder.cache(getCache());
        //设置https访问(验证证书)
//        okHttpClientBuilder.hostnameVerifier
//                (org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        //设置统一的header
        //okHttpClientBuilder.addInterceptor(getHeaderInterceptor());
//        Gson gson = new GsonBuilder()
//                .setDateFormat("yyyy-MM-dd hh:mm:ss")
//                .create();
        /*
        retrofit = new Retrofit.Builder()
                .baseUrl(UrlConstants.HOST_URL)
                .addConverterFactory(ResponseConverterFactory.create())//配置转化库，采用Gson
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//配置回调库，采用RxJava
                .client(okHttpClientBuilder.build())//设置OKHttpClient为网络客户端
                .build();
                */
        //service = retrofit.create(ApiService.class);
        //return retrofit ;
    }


    public <S> ApiService createService() {
        OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
        //设置请求超时时长
        okHttpClientBuilder.connectTimeout(30, TimeUnit.SECONDS);
        //启用Log日志
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.level(HttpLoggingInterceptor.Level.HEADERS);
        okHttpClientBuilder.addInterceptor(interceptor);
        //设置缓存方式、时长、地址
//        okHttpClientBuilder.addNetworkInterceptor(getCacheInterceptor());
//        okHttpClientBuilder.addInterceptor(getCacheInterceptor());
//        okHttpClientBuilder.cache(getCache());
        //设置https访问(验证证书)
        okHttpClientBuilder.hostnameVerifier
                (org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConstants.HOST_URL)
                .client(okHttpClientBuilder.build())
                .addConverterFactory(ResponseConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        return retrofit.create(ApiService.class);
    }


    // 创建网络请求Observable
    public static ApiService create() {
        return getInstance().retrofit.create(ApiService.class);
    }

    public static Retrofit initRetrofit() {

        OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
        //设置请求超时时长
        okHttpClientBuilder.connectTimeout(30, TimeUnit.SECONDS);
        //启用Log日志
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.level(HttpLoggingInterceptor.Level.BODY);
        okHttpClientBuilder.addInterceptor(interceptor);
        //设置缓存方式、时长、地址
//        okHttpClientBuilder.addNetworkInterceptor(getCacheInterceptor());
//        okHttpClientBuilder.addInterceptor(getCacheInterceptor());
//        okHttpClientBuilder.cache(getCache());
        //设置https访问(验证证书)
        okHttpClientBuilder.hostnameVerifier
                (org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        //设置统一的header
        //okHttpClientBuilder.addInterceptor(getHeaderInterceptor());
//        retrofit = new Retrofit.Builder()
//                .baseUrl(UrlConstants.HOST_URL)
//                .addConverterFactory(GsonConverterFactory.create())//配置转化库，采用Gson
//                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//配置回调库，采用RxJava
//                .client(okHttpClientBuilder.build())//设置OKHttpClient为网络客户端
//                .build();
        return null;

    }


    /**
     * 根据Api接口类生成Api实体
     *
     * @param clazz 传入的Api接口类
     * @return Api实体类
     */
//    public <T> T create(Class<T> clazz) {
//        checkNotNull(build, "BaseUrl not init,you should init first!");
//        return build.create(clazz);
//    }

    /**
     * 根据Api接口类生成Api实体
     *
     * @param baseUrl baseUrl
     * @param clazz   传入的Api接口类
     * @return Api实体类
     */
//    public <T> T create(String baseUrl, Class<T> clazz) {
//        return retrofit.baseUrl(baseUrl).build().create(clazz);
//    }

//    private <T> T checkNotNull(@Nullable T object, String message) {
//        if (object == null) {
//            throw new NullPointerException(message);
//        }
//        return object;
//    }
}
